/**
 *@author:xzy
 *
 * 这里定义一下注册时向后端传输的数据后
 * 各个数据的状态
 */


import {getToken, removeToken, setToken} from "../../utils/auth";
import {GET_USER_INFO, SET_NAME, SET_TOKEN, SET_USER_TYPE, SET_USERNAME} from "../EventConstant";
import {getUserInfo, login, logout ,modifyPwd,register} from "../../api/login";
import UserType from "../../common/userType";

const register ={
    state:{
        token:getToken(),
        username:'',
        password: '',
        passwordRepeat: '',
        name:'',         //姓名
        sex:'',
        address:'',     //设置地址
        QQ: '',
        phoneNumber:'',
    },
    mutations: {

    },
    actions:{
        register({commit},userInfo)
        {
            const username = userInfo.username.trim()
            return new Promise((resolve, reject) => {
                this.$alert("正在传输");
                register(username, userInfo.password, userInfo.name,userInfo.sex,userInfo.address,userInfo.QQ,userInfo.phoneNumber).then(response => {
                    const token = response.data.token;
                    commit(SET_TOKEN, token);
                    commit(SET_USERNAME, username);
                    commit(SET_USER_TYPE, userInfo.userType);
                    setToken(token);
                    resolve();
                }).catch(error => {
                    reject(error);
                })
            })
        }
    }
}
